package com.sjms._12_行为_代理模式._2_动态代理;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理类
 */
public class Son {
    private Object object;

    public void setTarget(Object object) {
        this.object = object;
    }

    public Object getProxy() {

        Object obj =
                Proxy.newProxyInstance(
                        object.getClass().getClassLoader(),//类加载器
                        object.getClass().getInterfaces(),//接口
                        //回调接口
                        new InvocationHandler() {
                            @Override
                            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                                //前置
                                System.out.println("儿子：爸爸我去吧，我出门啦！！！");

                                //执行原始方法
                                Object invoke = method.invoke(object, args);

                                System.out.println("爸爸：" + invoke);

                                //后置
                                System.out.println("儿子：爸爸，我回来了");

                                return "【肉x500g】【辣椒x100g】【洗涤剂x500ml】【煊赫门x2盒】";
                            }
                        });

        return obj;
    }

}
